<?php

namespace Admin\Controller;

class CaiDanController extends BaseController{
    // 菜单管理
    public function caiDan(){
        
        // 获取所有的菜单
        $data = M('caidan')->select();
        
        $caidan = three($data);
        
        $this->assign('caidan',$caidan);
        
        $this->display('Weixin/caiDan');
    }
    
     // 编辑菜单
    public function caidanEdit(){
        if(IS_POST){
            $data = I('post.');
            // 设置key
            $data['key'] = time().rand(1000,9999);
            
            $res = M('caidan')->save($data);
            
            if($res !== false){
                $this->ajaxReturn(array('status'=>1,'name'=>$data['name']));
            }else {
                $this->ajaxReturn(array('status'=>0,'msg'=>'修改数据失败，请稍后重试'));
            }
        }else {
            // 获取菜单id
            $id = I('get.id',0);
            
            if($id){
                // 查看当前菜单是否有子级菜单
                $res = M('caidan')->where(array('pid'=>$id))->getField('id');
                if ($res){ // 有子菜单
                    $haveSub = 1;
                }else{ // 没有字菜单
                    $haveSub = 2;
                }
                
                $this->assign('haveSub',$haveSub);
            }
            
            // 获取菜单信息
            $caidan = M('caidan')->find($id);
            
            $this->assign('caidan',$caidan);
            $this->assign('id',$id);
            
            $this->display('Weixin/caidanEdit');
        }
    }
    
    // 添加菜单
    public function addCaiDan(){
        // 获取父级id
        $pid = I('post.pid',0);
         
        if($pid == 0){ // 一级菜单
            $data = array(
                'name' => '菜单名称',
                'key'  => time().rand(1000,9999),
            );
        }else{ // 二级菜单
            $data = array(
                'name' => '子菜单',
                'pid'  => $pid,
                'key'  => time().rand(1000,9999),
            );
        }
        
        $res = M('caidan')->add($data);
        
        if($res){
            // 设置跳转路径
            $url = U('caidanEdit',array('id'=>$res));
            
            $this->ajaxReturn(array('status'=>1,'url'=>$url,'pid'=>$res));
        }else{
            $this->ajaxReturn(array('status'=>0));
        }
    }
    
    // 删除菜单
    public function caidanDel(){
        // 获取菜单id
        $id = I('get.id');
        
        // 删除菜单
        $res = M('caidan')->delete($id);
        if($res){
            // 删除子菜单
            M('caidan')->where(array('pid'=>$id))->delete();
            
            echo 1;die;
        }else {
            echo 0;die;
        }
    }
    
    // 同步数据到微信
    public function tongbu(){
        // 获取菜单数据
        $data = M('caidan')->select();
        $caidan = three($data);
        
        // 设置accessToken跟url
        $accessToken = getAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$accessToken;
        
        // 拼接请求参数
        $data = array();
        foreach ($caidan as $v){
            $res = array();
            if(!empty($v['sub'])){  // 有子菜单
                $res['name'] = $v['name'];
                
                foreach ($v['sub'] as $s){
                    $info = array();
                    if ($s['type'] == 1){ // 回复信息
                        $info['type'] = 'click';
                        $info['name'] = $s['name'];
                        $info['key'] = $s['key'];
                    }else { // 页面跳转
                        $info['type'] = 'view';
                        $info['name'] = $s['name'];
                        $info['url'] = $s['url'];
                    }
                    $res['sub_button'][] = $info;
                }
                
                
            }else { // 没有子菜单
                
                if ($v['type'] == 1){ // 回复信息
                    $res['type'] = 'click';
                    $res['name'] = $v['name'];
                    $res['key'] = $v['key'];
                }else { // 页面跳转
                    $res['type'] = 'view';
                    $res['name'] = $v['name'];
                    $res['url'] = $v['url'];
                }
            }
            $data['button'][] = $res;
        }
        
        // 将数组转为json数据
        $data = json_encode($data,JSON_UNESCAPED_UNICODE);
        
        // 发送网络请求
        $res = https_request($url,$data);
        $info = json_decode($res,true);
        
        if($info['errcode'] == 0){ // 设置成功
            echo 1;die;
        }else { // 设置失败
            echo 0;die;
        }
    }
}